package huoye.shops.huoye.mine.activity;

import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.codeideology.android.utils.StringUtils;
import com.codeideology.android.widget.convenientbanner.ConvenientBanner;
import com.codeideology.android.widget.convenientbanner.holder.CBViewHolderCreator;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import huoye.shops.R;
import huoye.shops.base.APIUrls;
import huoye.shops.base.BaseActivity;
import huoye.shops.huoye.home.adapter.BannerAdapter;
import huoye.shops.huoye.home.model.Banner;
import huoye.shops.huoye.mine.adapter.ShopMessageDetialsAdapter;
import huoye.shops.huoye.mine.model.ShopDetials;
import huoye.shops.huoye.mine.model.detialsImg;
import huoye.shops.huoye.mine.presenter.ShopMessagePresenter;
import huoye.shops.huoye.mine.view.IShopMessageView;
import huoye.shops.huoye.mine.widget.VersionsHintDialog;
import huoye.shops.huoye.operation.adapter.OrderDetailsShopMessageAdapter;
import huoye.shops.huoye.operation.widget.HintTitleDialog;
import huoye.shops.widget.RoundImageView;

import static java.security.AccessController.getContext;

public class ShopMessageActivity extends BaseActivity implements IShopMessageView{

    @BindView(R.id.shop_message_banner)
    ConvenientBanner convenientBanner;

    @BindView(R.id.shop_message_rv_head)
    RoundImageView rvHead;



    @BindView(R.id.shop_message_tv_shop_name)
    TextView tvShopName;

    @BindView(R.id.shop_message_tv_popularity)
    TextView tvPopularity;


    @BindView(R.id.iv_star1)
    ImageView ivStar1;
    @BindView(R.id.iv_star2)
    ImageView ivStar2;
    @BindView(R.id.iv_star3)
    ImageView ivStar3;
    @BindView(R.id.iv_star4)
    ImageView ivStar4;
    @BindView(R.id.iv_star5)
    ImageView ivStar5;


    @BindView(R.id.order_details_tv_contact)
    TextView tvShopContact;
    @BindView(R.id.order_details_tv_phone)
    TextView tvShopPhone;
    @BindView(R.id.order_details_tv_address)
    TextView tvShopAddress;



    //版本
    @BindView(R.id.shop_message_tv_versions)
    TextView tvShopVersions;
    //版本
    @BindView(R.id.shop_message_tv_expire_time)
    TextView tvShopExpireTime;
    //版本
    @BindView(R.id.shop_message_btn_versions_upgrade)
    Button btnShopVersionsUpgarade;




    @BindView(R.id.shop_message_rv_detials)
    RecyclerView rv;
    private ShopMessagePresenter mShopMessagePresenter;


    //版本升级
    @OnClick(R.id.shop_message_btn_versions_upgrade)
    void onVersionsUpgradeClick(){
        showVersionsUpgrade();
    }

    private void showVersionsUpgrade(){
        VersionsHintDialog hintTitleDialog = new VersionsHintDialog(this);
        hintTitleDialog.show();
    }


    //合同
    @OnClick(R.id.shop_message_btn_pact)
    void onPactClick(){
        startActivity(EnterPactActivity.class);
    }
    //编辑
    @OnClick(R.id.shop_message_btn_edit)
    void onEditClick(){
        startActivity(EditShopMessageActivity.class,mShopDetials);
    }


    @Override
    public void onResume() {
        super.onResume();
        if(mShopMessagePresenter !=null){
            mShopMessagePresenter.getShopDetials();
        }
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_shop_message;
    }

    @Override
    protected void initView(@Nullable Bundle bundle) {
        setTitle("店铺信息");
        mShopMessagePresenter = new ShopMessagePresenter();
        mShopMessagePresenter.addTaskListener(this);
        mShopMessagePresenter.onStart();
        mShopMessagePresenter.getShopDetials();
    }

    ArrayList<ImageView> stars = new ArrayList<>();
    //自定义RattingBar（根据服务器数据设置评分星星的数量）
    private void setStars(int level) {
        Log.e("test",level+"");
        stars.add(ivStar1);
        stars.add(ivStar2);
        stars.add(ivStar3);
        stars.add(ivStar4);
        stars.add(ivStar5);
        int score = level ;//评分（应该服务器获取，在此写死） //默认100
        for (int i = 0; i < score / 20; i++) {
            stars.get(i).setImageResource(R.mipmap.ic_popularity_star);
        }
        //设置半个星星
        if (score % 20 > 10) {
            stars.get(score / 20).setImageResource(R.mipmap.ic_popularity_star_not);
        }
    }

    ShopDetials mShopDetials;
    @Override
    public void setShopMessage(ShopDetials shopDetials) {
        this.mShopDetials = shopDetials;

        if(shopDetials.isBasicsVersion()){
            tvShopVersions.setText("基础版");
            tvShopVersions.setSelected(false);
            tvShopVersions.setBackgroundResource(R.mipmap.img_versions_basics);
            tvShopExpireTime.setText("到期时间 "+shopDetials.getShopEndTime());

            btnShopVersionsUpgarade.setVisibility(View.VISIBLE);
        }else {
            tvShopVersions.setText("豪华版");
            tvShopVersions.setSelected(true);
            tvShopVersions.setBackgroundResource(R.mipmap.img_versions_deluxe);
            tvShopExpireTime.setText("到期时间 "+shopDetials.getShopEndTime());
            btnShopVersionsUpgarade.setVisibility(View.GONE);
        }



        convenientBanner.setCanLoop(true);
        convenientBanner.startTurning(2000);
        convenientBanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });



        if(getContext() != null){
            Glide.with(this)
                    .load(APIUrls.COMMON_CONFIG.TYPE_COMMON_ALIYUN+shopDetials.getShopLogo())
                    .dontAnimate()
                    .placeholder(R.mipmap.ic_wait_logo)
                    .error(R.mipmap.ic_error)
                    .into(rvHead);
        }

        tvShopName.setText(shopDetials.getShopName());


        Integer focusNum = Integer.valueOf(shopDetials.getFocusNum());
        Integer virtualFoc = Integer.valueOf(shopDetials.getVirtualFoc());
        tvPopularity.setText("人气 "+(focusNum+virtualFoc));
        setStars(shopDetials.getStarLevel()*20);


        tvShopContact.setText(shopDetials.getContactName());
        tvShopPhone.setText(shopDetials.getContactTel());
        tvShopAddress.setText(shopDetials.getJointAddress());

        List<Banner> banners = new ArrayList<>();
        List<detialsImg> faceImg = shopDetials.getFaceImg();
        for (int i = 0; i < faceImg.size() ; i++){
            Banner banner = new Banner();
            banner.setImgUrl(APIUrls.COMMON_CONFIG.TYPE_COMMON_ALIYUN+faceImg.get(i).getImgUrl());
            banner.setTitle(faceImg.get(i).getImgDes());
            banners.add(banner);
        }

        setBanner(banners);


        ShopMessageDetialsAdapter adapter = new ShopMessageDetialsAdapter(this);
        if (shopDetials.getDetialsImg() != null) {
            adapter.setData(shopDetials.getDetialsImg());
        }
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(adapter);


    }


    public void setBanner(List<Banner> banner) {
        convenientBanner.setPages(
                new CBViewHolderCreator<BannerAdapter>() {
                    @Override
                    public BannerAdapter createHolder() {
                        BannerAdapter bannerAdapter = new BannerAdapter();
                        bannerAdapter.setOnBannerListener(new BannerAdapter.OnBannerListener() {
                            @Override
                            public void onBannerClick(Banner data) {
                                if (StringUtils.isNotEmpty(data.getUrl())) {
                                    // BaseTBWebViewActivity.startActivity(getActivity(), data.getTitle(), data.getUrl(), null);
                                }

                            }
                        });
                        return bannerAdapter;
                    }
                }, banner)
                .setPageIndicator(new int[]{R.mipmap.ic_page_indicator, R.mipmap.ic_page_indicator_focused});

    }


}
